package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
  @Autowired
  private DishMapper dishMapper;
  @Autowired
  private DishFlavorMapper dishFlavorMapper;
  @Autowired
  private SetmealDishMapper setmealDishMapper;
  
  /**
   * 分页查询
   * @param queryDTO
   */
  @Override
  public PageResult page(DishPageQueryDTO queryDTO) {
    PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
    Page<DishVO> page = dishMapper.page(queryDTO);
    return new PageResult(page.getTotal(), page.getResult());
  }
  
  /**
   * 新增菜品
   * @param dishDTO
   */
  @Override
  @Transactional(rollbackFor = {Exception.class}) // 添加事务
  public void add(DishDTO dishDTO) {
    Dish dish = new Dish();
    BeanUtils.copyProperties(dishDTO, dish);
    // 1.插入菜品信息
    dishMapper.insert(dish);
    System.out.printf("插入的菜品id："+ dish.getId());
    // 2.插入该菜品对应的口味
    List<DishFlavor> flavors = dishDTO.getFlavors();
    if(!flavors.isEmpty()) {
    // 批量设置菜品id
      flavors.forEach(f -> f.setDishId(dish.getId()));
      dishFlavorMapper.batchInsert(flavors);
    }
  }
  
  /**
   * 修改菜品
   * @param dishDTO
   */
  @Override
  @Transactional(rollbackFor = {Exception.class})
  public void update(DishDTO dishDTO) {
    Dish dish = new Dish();
    BeanUtils.copyProperties(dishDTO, dish);
    // 1.修改菜品基本信息
    dishMapper.update(dish);
    
    // 2.更新菜品口味
    // 2.1 先批量删除该菜品口味数据
    List<Long> dishIds = Arrays.asList(dishDTO.getId());
    dishFlavorMapper.batchDelete(dishIds);
    // 2.2 在批量保存该菜品口味数据
    List<DishFlavor> flavorList = dishDTO.getFlavors();
    flavorList.forEach(f -> f.setDishId(dish.getId())); // 设置菜品id
    dishFlavorMapper.batchInsert(flavorList);
  }
  
  /**
   * 批量删除
   * @param dishIds
   */
  @Override
  @Transactional
  public void batchDel(List<Long> dishIds) {
    // 判断该菜品是否能被删除? ->> 菜品被售卖中
    dishIds.forEach(id -> {
      DishVO dish = dishMapper.findById(id);
      if(dish.getStatus() == 1) {
        throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
      }
    });
    // 判断该菜品是否能被删除? ->> 菜品被套餐关联
    List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
    if(setmealIds != null && !setmealIds.isEmpty()) {
      throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
    }
    
    // 删除菜品数据
    dishMapper.batchDel(dishIds);
    // 删除菜品关联的口味数据
    dishFlavorMapper.batchDelete(dishIds);
  }
  
  /**
   * 起售停售菜品
   * @param status
   * @param id
   */
  @Override
  public void changStatus(Integer status, Long id) {
    Dish dish = Dish.builder().id(id).status(status).build();
    dishMapper.update(dish);
  }
  
  /**
   * 根据id查找
   * @param id
   */
  @Override
  public DishVO findById(Long id) {
    DishVO dish = dishMapper.findById(id);
    return dish;
  }
  
  /**
   * 根据分类id查询
   * @param categoryId
   */
  @Override
  public List<DishVO> findByType(Long categoryId) {
    List<DishVO> dishList = dishMapper.findByType(categoryId);
    return dishList;
  }
  
  /**
   * 条件查询菜品和口味
   * @param dish
   */
  @Override
  public List<DishVO> listWithFlavor(Dish dish) {
    List<Dish> dishList = dishMapper.list(dish);
    
    List<DishVO> dishVOList = new ArrayList<>();
    for (Dish d : dishList) {
      DishVO dishVO = new DishVO();
      BeanUtils.copyProperties(d, dishVO);
      
      // 根据菜品id查询对应口味
      List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
      dishVO.setFlavors(flavors);
      dishVOList.add(dishVO);
    }
    return dishVOList;
  }
}
